package com.cjh.controller;

import com.cjh.annotation.WebLog;
import com.cjh.pojo.Admin;
import com.cjh.pojo.User;
import com.cjh.utils.RedisUtil;
import com.cjh.utils.UploadService;
import com.cjh.vo.ResultMap;
import javafx.scene.Parent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * 上传图片接口
 */
@RestController
@RequestMapping("/uploadApi")
public class UploadController {

    @Autowired
    private UploadService uploadService;

    @Autowired
    private RedisUtil redisUtil;

//    @PostMapping("/upload")
//    @WebLog(description = "请求=>上传图片接口")
//    public ResultMap upload(@RequestParam(value = "uploadFile",required = false) MultipartFile uploadFile, HttpServletRequest request){
//
//        String realPath=request.getSession().getServletContext().getRealPath("/upload/");
//        File directory=new File(realPath);
//        if (!directory.exists()){
//            directory.mkdirs();
//        }
//        String oldName=uploadFile.getOriginalFilename();
//        String newName= UUID.randomUUID().toString()+oldName.substring(oldName.lastIndexOf(".",oldName.length()));
//        try {
//            uploadFile.transferTo(new File(directory,newName));
////            String filePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+"/uploadFile/"+newName;
//            String filePath="/upload/"+newName;
//
//            Map<String,Object> data=new HashMap<>();
//            data.put("src",filePath);
//            return ResultMap.ok(0,"上传图片请求",data);
//        } catch (IOException e) {
//            e.printStackTrace();
//            return ResultMap.fail(1,"上传图片请求","服务器异常");
//        }
//
//    }

    /**
     * FastDfs服务器文件上传
     *
     * @param uploadFile
     * @return
     */
    @PostMapping("/upload/{status}")
    @WebLog(description = "请求=>上传图片接口")
    public ResultMap upload(@RequestParam(value = "uploadFile", required = false) MultipartFile uploadFile,
                            @PathVariable(value = "status", required = false) String status,
                            HttpSession session) {
        if (!ObjectUtils.isEmpty(status)) {
            //0:普通用户
            if (Objects.equals(status, "0")) {

                User user = (User) session.getAttribute("user");
                if (!ObjectUtils.isEmpty(user)) {
                    Integer id=user.getId();

                    return doUpload(id,uploadFile);
                }

                //1:管理员
            } else if (Objects.equals(status, "1")) {

                Admin admin = (Admin) session.getAttribute("admin");
                if (!ObjectUtils.isEmpty(admin)) {
                    Integer id=admin.getId();

                    return doUpload(id,uploadFile);

                }

            }

        }
        return ResultMap.ok(500, "上传图片请求","请求参数异常");

    }

    public ResultMap doUpload(Integer id,MultipartFile uploadFile){
        //第一次点击上传
        if (ObjectUtils.isEmpty(redisUtil.get("uploadWaitTime:" + id))) {
            //图片上传到FastDfs
            String filePath = uploadService.uploadImage(uploadFile);
            //redis设置一个唯一的时间限制缓存(携带id)，避免恶意无限上传图片
            redisUtil.set("uploadWaitTime:" + id, "限制无限上传图片", 20);
            Map<String, Object> data = new HashMap<>();
            data.put("src", filePath);
            return ResultMap.ok(0, "上传图片请求", data);
        } else {
            return ResultMap.ok(1, "上传图片请求", "操作过于频繁，请" + (int) redisUtil.getExpire("uploadWaitTime:" + id) + "秒后重试");
        }
    }
}


